<?php
header('Content-Type:text/plain');
$cache = new Cache(3600,"chache");
$cdn = "ws";
if(isset($_GET["cdn"])){
    $cdn=$_GET["cdn"];
}
$cache_key=sprintf("cctv4k-cache-%s",$cdn);
$playUrl = $cache->get($cache_key);
$headers = ["User-Agent: cctv_app_tv","Referer: api.cctv.cn","UID: 1314520"];
if (!$playUrl){
    $URL = "https://ytpvdn.cctv.cn/cctvmobileinf/rest/cctv/videoliveUrl/getstream";
    $post=sprintf('appcommon={"ap":"cctv_app_tv","an":"央视投屏助手","adid":"1314520","av":"1.1.7"}&url=http://live%s-tp4k.cctv.cn/live/4K0219.stream/playlist.m3u8', $cdn);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch,CURLOPT_TIMEOUT,30);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $data = curl_exec($ch);
    curl_close($ch);
    $jx = json_decode($data);
    $playUrl = $jx->url;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $playUrl);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);

$lines = preg_split("/\n/", $data);
foreach ($lines as $line){
  if (strpos($line, "m3u8")){
    $playUrl = sprintf("http://live%s-tp4k.cctv.cn/live/4K0219.stream/%s", $cdn, $line);
    break;
  }
}
$cache->put($cache_key, $playUrl);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $playUrl);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);

$data = preg_replace('/(.*?.ts)/i', sprintf("http://live%s-tp4k.cctv.cn/live/4K0219.stream/$1", $cdn), $data);
header("Content-Type:audio/x-mpegurl");
header("Content-Disposition:attachment;filename=cctv4k.m3u8");
die($data);


class Cache {

        private $cache_path;
        private $cache_expire;
        public function __construct($exp_time=3600,$path="cache/"){
            $this->cache_expire=$exp_time;
            $this->cache_path=$path;
        }

        private function fileName($key){  return $this->cache_path.md5($key); }
        public function put($key, $data){
            $filename = $this->fileName($key);
            $file = fopen($filename, 'w');
            if ($file){

                fwrite($file, $data);
                fclose($file);
            }
            else return false;
        }
        public function get($key){
            $filename = $this->fileName($key);
            if (!file_exists($filename) || !is_readable($filename)){ return false; }
            if ( time() < (filemtime($filename) + $this->cache_expire) ) {
                $file = fopen($filename, "r");
                if ($file){
                    $data = fread($file, filesize($filename));
                    fclose($file);
                    return $data;
                }
                else return false;
            }
            else return false;
        }
    }
?>
